$.bsgrid.forcePushPropertyInObject(
	$.fn.bsgrid.defaults.extend.renderPerRowMethods,
	'extend_render_per_row', function(record, rowIndex, trObj, options) {
		if (record != undefined && record != '') {
//				trObj.removeAttr('staffId');
			trObj.attr("ischecked",record.isChecked);
			trObj.attr("info",record.orgName+" "+record.postName);
		}
	});
$(document).ready(function(){
	var token=localStorage.getItem("token");
	var addUser=localStorage.getItem("username");
	//标明从哪个页面跳转到分配岗位页，source=list，员工列表页；source=edit，员工编辑页
	var source=tl_config.his_url.split("&")[1].split("=")[1];
	if("list"==source){
		$("div .z_title").html('首页&gt;员工管理&gt;分配岗位');
	}
	if("edit"==source){
		$("div .z_title").html('首页&gt;员工管理&gt;编辑员工&gt;分配岗位');
	}
	var id = tl_config.his_url;
	id = id.substring(id.indexOf("id=")+3, id.length);
	id = id.substring(0, id.indexOf("&"));

	gridObj = $.fn.bsgrid.init('g_tabYeh', {
		ajaxType : 'get',
		url : server_url+'rest/assignPost.do?token='+token+'&staffId='+id+'&ran='+Math.random(),
		autoLoad : true,
		pageSizeSelect : true,
		stripeRows : true,
		pageSize : 10,
		displayBlankRows : false,
		isProcessLockScreen : true,
		rowSelectedColor : false,
		complete: function(){
			init();
		}
	});


	$(".z_sure,.l_bcgonext").click(function(){
		var postIds=gridObj.getCheckedValues('id').toString();
		var flag=true;
		//未勾选岗位，提示
//		if(postIds==''){
//			easyDialog.open({
//				container:{
//					header:'温馨提示',
//					content:"请为员工分配岗位！"
//				},
//				autoClose : 2000
//			});
//			return;
//		}

		var target=$(this).html();
		var newChecks=new Array();
		if(postIds!=''){
			newChecks=postIds.split(",");
		}
		//原先已分配岗位
		var oldChecks=[];
		var trLi=$("#g_tabYeh tbody tr");
		for(var i=0;i<trLi.length;++i){
			if($(trLi[i]).attr("ischecked")=="true"){
				oldChecks.push($($(trLi[i]).find("input")[0]).val());
			}
		}

		//新增的岗位id
		var adds=[];
		//删除的岗位id
		var dels=[];
		//未改变的岗位id
		var pubs=[];

		for(var i in newChecks){
			for(var j in oldChecks){
				if(newChecks[i]==oldChecks[j]){
					pubs.push(newChecks[i]);
					break;
				}
			}
		}

		if(pubs.length!=0){
			for(var i in newChecks){
				for(var j in pubs){
					if(newChecks[i]!=pubs[j]){
						adds.push(newChecks[i]);
						break;
					}
				}
			}

			for(var i in oldChecks){
				for(var j in pubs){
					if(oldChecks[i]!=pubs[j]){
						dels.push(oldChecks[i]);
						break;
					}
				}
			}
		}else{
			adds=newChecks;
			dels=oldChecks;
		}

		//判断用户是否有岗位
		//当前页没有新增岗位
		if(adds.length==0){
			$.ajax({
				type:'get',
				url:server_url+'rest/isPostExistByStaffId.do?token='+token+'&staffId='+id+'&ran='+Math.random(),
				data:{
					dels:dels
				},
				async:false,
				traditional:true,
				success:function(data){
					if(data.retCode=='0000'){
						if(data.data=='1'){
							flag=true;
						}
						if(data.data=='0'){
							flag=false;
						}
					}
				}
			});
		}
		if(flag==false){
			easyDialog.open({
				container:{
					header:'温馨提示',
					content:"请为员工分配岗位！"
				},
				autoClose : 2000
			});
			return;
		}

		var addLog='';
		//新增log
		var inputs=$("#g_tabYeh tbody tr input");
		for(var i in adds){
			for(var j in inputs){
				if($(inputs[j]).val()==adds[i]){
					addLog+=$($(inputs[j]).parent().parent()).attr("info")+",";
					break;
				}
			}
		}

		var delLog='';
		//删除log
		for(var i in dels){
			for(var j in inputs){
				if($(inputs[j]).val()==dels[i]){
					delLog+=$($(inputs[j]).parent().parent()).attr("info")+",";
					break;
				}
			}
		}

		var log='';
		if(addLog!=''){
			log+='新增 '+addLog;
		}

		if(delLog!=''){
			log+='删除 '+delLog;
		}

		log=log.substring(0,log.length-1);

		$.ajax({
			type:'post',
			url:server_url+'rest/savePost.do?ran='+Math.random(),
			data:{
				token:token,
				staffId:id,
				addUser:addUser,
				adds:adds,
				dels:dels,
				log:log
			},
			traditional:true,
			success:function(data){
				if(data.retCode=="0000"){
					if(target=='确定'){
						var url="staffList.do?ran="+Math.random();
						tl_config.loadToWorkArea(url);
					}else{
						//分配数据权限
						var url="assignPrecinct.do?id="+id+"&source=assignPost&ran="+Math.random();
						tl_config.loadToWorkArea(url);
					}
				} else {
					easyDialog.open({
						container:{
							header:'温馨提示',
							content:data.retMsg
						},
						autoClose : 2000
					});
				}
			}
		});
	});

	$(".l_return").click(function(){
		if("edit"==source){
			var url="editStaff.do?id="+id+ "&ran="+Math.random();
			tl_config.loadToWorkArea(url);
		}
		if("list"==source){
			var url="staffList.do?id="+id+ "&ran="+Math.random();
			tl_config.loadToWorkArea(url);
		}
	});

	//已分配岗位初始化
	function init(){
		var trLi=$("#g_tabYeh tbody tr");
		for(var i=0;i<trLi.length;++i){
			if($(trLi[i]).attr("ischecked")=="true"){
				$(trLi[i]).find("input").attr("checked","checked");
			}
		}
	}

});


//数据表格参数查询
function doSearch() {
	searchParames = $('#form').serialize();
	gridObj.search(searchParames);
}




